module fsm_control_ex_3d(
    input clk,
    input reset,
	input press,
    input[4:1] W,
    output reg [2:0] lights_c,
    output reg [2:0] lights_p,
    output reg[4:1] C
);

    // FSM state declaration
    reg [2:0] current_state, next_state;
    parameter 	S0 = 3'b000,
              	S1 = 3'b001,
              	S2 = 3'b011,
				S3 = 3'b010,
              	S4 = 3'b110,
				S5 = 3'b100;

    // State register
    always @(posedge clk or posedge reset) begin
        if (reset)
            current_state <= S0;
        else
            current_state <= next_state;
    end

    // Next state logic
    always @(*) begin
        case (current_state)
            S0: next_state = press ? S1 : S0;
            S1: next_state = W[1] ? S2 : S1;
            S2: next_state = W[2] ? S3 : S2;
            S3: next_state = W[3] ? S4 : S3;
			S4: next_state = W[2] ? S5 : S4;
			S5: next_state = W[4] ? S0 : S5;
            default: next_state = S0;
        endcase
    end


// Output logic
/*
c车灯	p人灯
100绿	010黄    001红
*/
always @(posedge clk or posedge reset) begin
    if (reset) begin
        C = 4'b0000;
        lights_c = 3'b100; // S0车绿
        lights_p = 3'b001; // 人红
    end else begin
        case(current_state)
            S0: begin
                lights_c = 3'b100; 
                lights_p = 3'b001; 
            end
            S1: begin
                lights_c = 3'b100; 
                lights_p = 3'b001; 
            end
            S2: begin
                lights_c = 3'b010; 
                lights_p = 3'b001; 
            end
            S3: begin
                lights_c = 3'b001;
                lights_p = 3'b100;
            end
            S4: begin
                lights_c = 3'b001;
                lights_p = 3'b010;
            end
            S5: begin
                lights_c = 3'b100;
                lights_p = 3'b001;
            end
            default: begin
                lights_c = 3'b100;
                lights_p = 3'b001;
            end
        endcase
    end
end



	/*
    assign C[1] = (current_state == S1);
    assign C[2] = (current_state == S2 || current_state == S4);
    assign C[3] = (current_state == S3);
	assign C[4] = (current_state == S5);
    assign lights_c[2] = (current_state == S0||current_state == S1||current_state == S5);
    assign lights_c[1] = (current_state == S2);
    assign lights_c[0] = (current_state == S2 || current_state == S3);
    assign lights_p[2] = (current_state == S3);
    assign lights_p[1] = (current_state == S4);
    assign lights_p[0] = (current_state == S0||current_state == S1||current_state == S2||current_state == S5);
	*/
	

endmodule
